package com.chinadrtv.batch.admin.jobs.ozing.gift;

import com.chinadrtv.batch.admin.jobs.ozing.common.OzingCardTransformer;
import com.chinadrtv.user.dao.ozing.OzingCardDao;
import com.chinadrtv.user.model.*;
import com.chinadrtv.user.service.ozing.OzingCardService;
import com.chinadrtv.user.util.CardUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Jack Chen
 *         Date: 12-12-18
 */
public class OzingCardProcessor implements ItemProcessor<Device, OzingCardTransformer> {
    private static Log log = LogFactory.getLog(OzingCardProcessor.class);

    final SimpleDateFormat sf = new SimpleDateFormat("yyMMdd");
    private int count = 0;

    private long cardValue;
    private int period;
    private String cardType;
    private String periodType;
    private String source;

    @Autowired
    private OzingCardDao ozingCardDao;

    public OzingCardTransformer process(Device device) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("*************OzingCardJob--Processor is running*************");
        }
        Date today = new Date();
        Device device_  = ozingCardDao.findDeviceByID(device.getId());
        boolean  valid = false;
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -30);
        if (device_.getGiftCardHisList() != null && device_.getGiftCardHisList().size() > 0) {
            DeviceOzingGiftCardHis latestHis = device_.getGiftCardHisList().get(0);
            if (latestHis.getDateAdded().before(calendar.getTime())) {
                valid = true;
            }
        }else{
            valid = true;
        }
        if(valid){
            String password = CardUtil.generateCardPassword(today);
            int i=0;
            while (ozingCardDao.existsPassword(password) && i<100) {
                password = CardUtil.generateCardPassword(today);
                i++;
            }

            String cardIndex_ =  password;  //赠送的卡用passwd作为index, 防止重复.

            String period_ = ("000" + period).substring(String.valueOf(period).length());
            String cardId = cardIndex_ + period_ + periodType; //生成卡号

            OzingCard ozingCard = new OzingCard();
            ozingCard.setActive(true);
            ozingCard.setCardActivationDeadline(today);
            ozingCard.setCardId(cardId);
            ozingCard.setCardPassword(password);
            //ozingCard.setCardIndex(cardIndex);
            ozingCard.setCardParValue(getCardValue());
            ozingCard.setCardType(CardType.valueOf(cardType));
            //ozingCard.setCardStatus()
            ozingCard.setCardValidityPeriod(period);
            ozingCard.setCardValidityPeriodType(OzingCard.CardPeriodType.valueOf(periodType));
            ozingCard.setCreatedBy(device_.getUser().getUsername());
            ozingCard.setDateAdded(today);
            ozingCard.setSource(source);

            OzingCardTransformer ozingCardTransformer = new OzingCardTransformer();
            ozingCardTransformer.setUser(device_.getUser());
            ozingCardTransformer.setOzingCard(ozingCard);
            ozingCardTransformer.setDevice(device_);
            return ozingCardTransformer;
        }else{
            return new OzingCardTransformer();
        }
    }

    public long getCardValue() {
        return cardValue;
    }

    public void setCardValue(long cardValue) {
        this.cardValue = cardValue;
    }

    public int getPeriod() {
        return period;
    }

    public void setPeriod(int period) {
        this.period = period;
    }

    public String getCardType() {
        return cardType;
    }

    public void setCardType(String cardType) {
        this.cardType = cardType;
    }

    public String getPeriodType() {
        return periodType;
    }

    public void setPeriodType(String periodType) {
        this.periodType = periodType;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public OzingCardDao getOzingCardDao() {
        return ozingCardDao;
    }

    public void setOzingCardDao(OzingCardDao ozingCardDao) {
        this.ozingCardDao = ozingCardDao;
    }
}
